package org.zxp.designpattern.observer.javaapi;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

/**
 * @program: effectiveJava
 * @description:被观察者
 * @author: X-Pacific zhang
 * @create: 2019-08-29 11:31
 **/
public class Watched {
    private PropertyChangeSupport listeners = new PropertyChangeSupport(this);
    private int value = 100;

    public void addWatcher(PropertyChangeListener listener){
        listeners.addPropertyChangeListener(listener);
    }

    public void change(int a){
        int newValue = value - a;
        listeners.firePropertyChange("Watchedzxp", value, newValue);
    }

    public static void main(String[] args){
        Watched watched = new Watched();
        watched.addWatcher(new Watcher());
        watched.change(30);
    }
}
